//
//  LikersViewController.m
//  Buzz API Demo
//
//  Created by Peter Hopkins on 5/13/10.
//  Copyright 2010 Google Inc. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "LikersViewController.h"


#import "Author.h"
#import "BuzzDao.h"

@implementation LikersViewController

- (id)initWithDao:(BuzzDao *)dao post:(Post *)post {
  if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
    dao_ = [dao retain];
    post_ = [post retain];
  }

  return self;
}


- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  likersResults_ = [[dao_ fetchLikersForPost:post_ delegate:self] retain];
  [dao_ syncLikers:post_];
}


- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];

  [likersResults_ release];
  likersResults_ = nil;
}


#pragma mark NSFetchedResultsController methods


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  [[self tableView] reloadData];
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSArray *sections = [likersResults_ sections];
  if ([sections count] == 0) {
    return 0;
  }

  id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
  return [sectionInfo numberOfObjects];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
  static NSString *CellIdentifier = @"Cell";
  
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];
  }
  
  Author *author = [likersResults_ objectAtIndexPath:indexPath];
  [[cell textLabel] setText:[author name]];
  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
	
  return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  Author *author = [likersResults_ objectAtIndexPath:indexPath];

  UIWebView* profileView = [[[UIWebView alloc] init] autorelease];
  [profileView setScalesPageToFit:YES];
  [profileView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[author profileURL]]]];

  UIViewController *profileController = [[[UIViewController alloc] init] autorelease];
  [profileController setView:profileView];
  [profileController setTitle:[author name]];

  [[self navigationController] pushViewController:profileController animated:YES];

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)dealloc {
  [dao_ release];
  [post_ release];
  [likersResults_ release];

  [super dealloc];
}


@end

